#include <iostream>
#include <signal.h>
#include <sys/types.h>

void PrintPeding(sigset_t &s)
{
    std::cout << "当前进程pid:" << getpid() << "进程Pending表:";
    for (int i = 31; i >= 1; i--)
    {
        if (sigismember(&s, i))
        {
            std::cout << 1;
        }
        else
        {
            std::cout << 0;
        }
    }

    std::cout << std::endl;
}

void handler(int args)
{

    std::cout << "######################################" << std::endl;
    std::cout << "当前进程进入递达状态" << std::endl;
    sigset_t s;
    sigpending(&s);
    PrintPeding(s);
    std::cout << "######################################" << std::endl;
    exit(1);
}

int main()
{
    signal(SIGINT, handler);

    sigset_t sig, old;
    sigemptyset(&sig);
    sigemptyset(&old);
    sigaddset(&sig, SIGINT);

    sigprocmask(SIG_BLOCK, &sig, &old);

    int cnt = 10;
    sigset_t s;
    sigemptyset(&s);
    while (1)
    {
        sigpending(&s);
        PrintPeding(s);
        cnt--;
        sleep(1);
        if (cnt == 0)
        {
            std::cout << "解除信号的屏蔽" << std::endl;
            sigprocmask(SIG_SETMASK, &old, &sig);
        }
    }

    return 0;
}
